package com.ruoyi.info.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.info.domain.CmsApp;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.util.security.WxqbUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.info.domain.CmsQb;
import com.ruoyi.info.service.ICmsQbService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 威胁情报-搜索记录Controller
 * 
 * @author xwj
 * @date 2023-08-23
 */
@Controller
@RequestMapping("/cms/qb")
public class CmsQbController extends BaseController
{
    private String prefix = "cms/qb";

    @Autowired
    private ICmsQbService cmsQbService;
    @Autowired
    private ISysConfigService configService;

    @GetMapping()
    public String qb()
    {
        return prefix + "/qb";
    }

    /**
     * 查询威胁情报-搜索记录列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CmsQb cmsQb)
    {
        startPage();
        List<CmsQb> list = cmsQbService.selectCmsQbList(cmsQb);
        return getDataTable(list);
    }

    /**
     * 导出威胁情报-搜索记录列表
     */
    @Log(title = "威胁情报-搜索记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsQb cmsQb)
    {
        List<CmsQb> list = cmsQbService.selectCmsQbList(cmsQb);
        ExcelUtil<CmsQb> util = new ExcelUtil<CmsQb>(CmsQb.class);
        return util.exportExcel(list, "威胁情报-搜索记录数据");
    }

    /**
     * 新增威胁情报-搜索记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存威胁情报-搜索记录
     */
    @Log(title = "威胁情报-搜索记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsQb cmsQb)
    {
        return toAjax(cmsQbService.insertCmsQb(cmsQb));
    }

    /**
     * 修改威胁情报-搜索记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsQb cmsQb = cmsQbService.selectCmsQbById(id);
        mmap.put("cmsQb", cmsQb);
        return prefix + "/edit";
    }

    /**
     * 修改保存威胁情报-搜索记录
     */
    @Log(title = "威胁情报-搜索记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsQb cmsQb)
    {
        return toAjax(cmsQbService.updateCmsQb(cmsQb));
    }

    /**
     * 删除威胁情报-搜索记录
     */
    @Log(title = "威胁情报-搜索记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cmsQbService.deleteCmsQbByIds(ids));
    }


    @PostMapping("/listPage")
    @ResponseBody
    public TableDataInfo listPage(CmsQb cmsQb) {
        startPage();
        SysUser currUser = getSysUser();
        cmsQb.setUserId(currUser.getUserId());
        List<CmsQb> list = cmsQbService.selectCmsQbList(cmsQb);
        return getDataTable(list);
    }

    @GetMapping("/wxqbQuery")
    @ResponseBody
    public AjaxResult wxqbQuery(@RequestParam Map<String, Object> params) throws Exception {
        String appid = configService.selectConfigByKey("wxqb2.appid");
        String secret = configService.selectConfigByKey("wxqb2.secret");
        String thirduid = configService.selectConfigByKey("wxqb2.thirduid");

        SysUser currUser = getSysUser();
        String type = Convert.toStr(params.get("type"));
        String value = Convert.toStr(params.get("value"));

        // 保存记录
        CmsQb cmsQb = new CmsQb();
        cmsQb.setType(type);
        cmsQb.setContent(value);
        cmsQb.setUserId(currUser.getUserId());

        List<CmsQb> exists = cmsQbService.selectCmsQbList(cmsQb);
        if(exists == null || exists.size() == 0) {
            cmsQbService.insertCmsQb(cmsQb);
        }

        String token =  WxqbUtil.genToken(appid, secret, thirduid);

        Map<String, String> result = new HashMap<>();
        result.put("token", token);
        result.put("appid", appid);
        result.put("secret", secret);
        result.put("thirduid", thirduid);

        return AjaxResult.success(result);
    }
}
